<?php

class module {
		
		function __contstruct() {
			
			
		}
		function __destruct() {
			
			
		}
		function mid() {
			// this module ID return it then they can add it to the forms	
		}
		function echoModuleURI() {
			// returns a full URI including the site URL etc
			
			return $this;	
		}
		function echoModulePath() {
			// returns file system path of the module	
		
			return $this;
		}
		function install($path,$filename) {
			$admin=new admin();
			
			//$module_id=new MongoId();
			$tn=explode(".",$filename);
			// will just overwrite...
			$t=$_SERVER['DOCUMENT_ROOT'].'/modules/'.$tn[0];
			mkdir($t,0777);
			
		 $zip = new ZipArchive;
     	$res = $zip->open($path);
     	$c=false;
	 if ($res === TRUE) {
         $zip->extractTo($t);
         $zip->close();
         $c=true;
	 } 
		if($c) {
		//	$admin->zip_flatten($path,$t);
			
			$settings=json_decode(file_get_contents($t.'/config.json'),true);
			
			
			if(empty($settings['name'])) {
				return false;	
			}
			if(!file_exists($t.'/index.php')) {
				return false;	// need some better error checking here to say what's going on dude..
			}
			$data= new database();
			$data->db(DATABASE)->collection('modules');
			$j=$settings;
			$item=$j;
			$item['_id']=new MongoId();
			$item['path']=$tn[0];
			$item['active']=(int)1;
			$data->criteria($item);
			$row=$data->insert();
			$data->close();
			// build the 2 bundles...
			$data=new database();
			$data->db(DATABASE)->collection('admin_navigation');
			$data->criteria(array('label'=>'Modules'));
			$m=$data->findOne();
			$module_parent=(string)$m['_id'];
			
			
			
			$data=new database();
			$data->db(DATABASE)->collection('admin_navigation');
			print_r($item['admin']);
			foreach($item['admin']['navigation'] as $anav) {
			echo("Admin Nav Name: ".$anav);
			$data->criteria(array('label'=>$item['name']. ' (module)',
	  'href'=>'/modules/'.$tn[0].'/'.$anav,
	  'order'=>intval(0),	
	  'is_parent'=>intval(0),
	  'parent'=>$module_parent,
	  'id'=>'list-'.(string)$item['_id'].'-settings',
	  'active'=>intval(1)
))->debug()->insert();
	///// add to admin pages
$data->collection('admin_pages');
$gid=$admin->getAdminGroup();
foreach($item['admin']['functions'] as $afunc) {
	$data->criteria(
	array('groups'=>array($gid),
	'name'=>'Module functions ('.$afunc.')',
	'path'=>'/modules/'.$tn[0].'/'.$afunc,
	'role'=>'functionality'
	))->debug()->insert();
	
}
$admin->createClientBundle()->createAdminBundle();

			}
			return $row;
		} else {
			return false;	
		}
		}
		function getContents($filename,$row,$json) {
			foreach($row['settings'] as $key=>$val) {
				//echo("Setting settings=>$key to ".$json['settings'][$key]."");
			//	// dont allow overwrite of the orginal in the main ROW - security exposure
				if(!empty($json['settings'][$key])) {
					$row['settings'][$key]=$json['settings'][$key];
					
				}
			}
			$row['module_path']='/modules/'.$row['path'];
			if (is_file($filename)) {
				ob_start();
			$_settings=$row;
			include $filename;
			return ob_get_clean();
			}
			return false;
		}
		function parse($json_str) {
			$json = json_decode($json_str,true);
			$id=$json['id'];
			$data = new database();
			$data->db(DATABASE)->collection('modules');
			$data->criteria(array('_id'=>new MongoId($id),'active'=>intval(1) ));
			$row=$data->findOne();
			$data->close();
			
			$site->page_settings['_module']=$row; // no good if it's cached
			// if there is settings that are overridden then they should be here too and go over the $row['settings'] array
			// for example ... $row['settings']['keywords']
			
			if(!$row['_id']) {
				$output=''; // just removes the module text from the string	
			} else {
			$module_settings=$row;
			$path=$_SERVER['DOCUMENT_ROOT'].'/modules/'.$row['path'];
			
			
			//print_r($output);
			$output=$this->getContents($path.'/index.php',$row,$json);
			
			//var_dump($output);
			}
			return $output;
		}
		function saveModule($id,$criteria) {
			$data=new database();
			$data->db(DATABASE)->collection('modules');
			$data->criteria($criteria);
			$data->update($id);
			$data->close();
			return $this;
		}
		function getSettings($path) {
			
			$id=end(explode("/",$path));
			$data = new database();
			$data->db(DATABASE)->collection('modules');
			$data->criteria(array('_id'=>new MongoId($id)));
			$row=$data->findOne();
			$data->close();
			return $row;
		}
}

?>